package com.itheima.sparkml.features

import org.apache.spark.SparkConf
import org.apache.spark.ml.feature.{StringIndexer, StringIndexerModel}
import org.apache.spark.sql.{DataFrame, SparkSession}

object _1StringIndexer {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("_5LibSvmLoaderSQL").setMaster("local[*]")
    val spark: SparkSession = SparkSession.builder().config(conf).getOrCreate()
    spark.sparkContext.setLogLevel("WARN")
    val df = spark.createDataFrame(
      Seq((0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c"))
    ).toDF("id", "category")
    df.show()
    val indexer = new StringIndexer().setInputCol("category").setOutputCol("cateIndexer")
    val indexerModel: StringIndexerModel = indexer.fit(df)
    // * 4-特征转换
    val dfResult: DataFrame = indexerModel.transform(df)
    // * 5-结果展示
    dfResult.show(false)
  }
}
